package com.spj.shiro;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.env.BasicIniEnvironment;
import org.apache.shiro.env.Environment;
import org.apache.shiro.mgt.SecurityManager;
import org.apache.shiro.subject.Subject;

/**
 * @author :spj
 * @Date:2022/4/13
 * @description:
 */
public class Demo {
    public static void main(String[] args) {
        Environment environment = new BasicIniEnvironment("classpath:shiroConfig/shiro-realm.ini");
        SecurityManager securityManager = environment.getSecurityManager();
        SecurityUtils.setSecurityManager(securityManager);
        Subject subject = SecurityUtils.getSubject();
        try {
            subject.login(new UsernamePasswordToken("spj", "123"));
            if(subject.isAuthenticated()){
                System.out.println(subject.getPrincipal()+"登录成功");
                System.out.println("身份为user吗"+subject.hasRole("user"));
                System.out.println("可以吃吗"+subject.isPermitted("user:eat"));
            }else{
                System.out.println("登陆失败");
            }
        } catch (Exception e) {

        }

//        if (subject.isPermitted("user:find")) {
//            System.out.println("this is user");
//        } else {
//            System.out.println("this is boss");
//        }
    }
}

